package com.app.system.config.task;

import com.alibaba.druid.support.json.JSONUtils;
import com.app.business.en.Status;
import com.app.business.model.Contract;
import com.app.business.service.ContractService;
import com.app.business.service.StatisticsForMainService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.exception.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by wcf-pc on 2018/4/23.
 */
@Component
public class TaskDemo {
    @Autowired
    private StatisticsForMainService statisticsForMainService;
    @Autowired
    private ContractService contractService;

    /**
     * 每分钟执行一次
     */
    @Transactional
    @Scheduled(cron = "0 0/1 * * * *")
    public void timer() {
        List<Contract> contractList = this.contractService.getNoBzContractList();
        if (CollectionUtils.isNotEmpty(contractList)) {
            for (Contract contract : contractList) {
                contract.setBzYyStatus(Status.NO.getIndex());
                contract.setBzYyDate(null);
                contract.setTime(null);
                this.contractService.save(contract);
                System.out.println("-------自动撤销预约已预约未办证信息--------");
            }
        }
        System.out.println("惠安选房综合服务管理平台");
    }

    /**
     * 5分钟执行一次
     * 10秒： 0/10 * * * * *
     * 5分钟：0 0/5 * * * *
     */
    /*@Scheduled(cron = "0 0 0/4 * * *")
    public void timer1() {
        this.main();
    }*/

   /* private void main() {
        System.out.println("数据推送中...");
        RestTemplate restTemplate = new RestTemplate();
        //主系统验证权限路径
//        String url = "https://tszs.bjjmdf.com/app/mobileReceiveData/receiveData";
        String url = "http://192.168.50.171:8087/app/mobileReceiveData/receiveData";
        HttpMethod method = HttpMethod.POST;
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        //安置数据推送
        List<Map> dataList = this.statisticsForMainService.getDataForMain();
        params.add("dataList", JSONUtils.toJSONString(dataList));

        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<Response> responseEntity = null;
        try {
            responseEntity = restTemplate.exchange(url, method, requestEntity, Response.class);
            Response response = responseEntity.getBody();
            if (response.getCode().equals("0000")) {//验证通过
                System.out.println("**************************" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + ":************************");
                System.out.println(response.getMessage());
            } else {
                throw new SQLException(response.getMessage());
            }
        } catch (Exception e) {
        }
    }*/
}
